package com.geek.novel.services

import com.geek.novel.common.Constant
import com.geek.novel.entity.SysFile
import com.geek.novel.network.RslHandler
import com.geek.novel.network.RepositoryProvider
import com.geek.novel.repository.HttpRepository
import com.geek.novel.utils.SecurityUtil
import com.geek.novel.view.loading.LoadingDialogUtil
import java.io.File

/**
 * File相关Service
 */
class FileService(rslHandler: RslHandler): BaseService(rslHandler) {

    private var repository: HttpRepository = RepositoryProvider.provideRepository()

    /**
     * 上传图片
     */
    fun uploadFile(file: File, fileType: String, fileId: Long?, fileName: String?=null, filePath: String?=null) {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("文件上传中...")
        //调用上传接口
        val observer = repository.uploadFile(file, "text/plain", fileType, fileId, fileName, filePath)
        compose(observer, Constant.HTTP_KEY_UPLOAD_FILE, SysFile::class.java)
    }


    /**
     * 上传图片
     */
    fun uploadImageFile(file: File) {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("头像上传中...")
        //入参
        val user = SecurityUtil.getPrincipal()
        //调用上传接口
        val observer = repository.uploadFile(file, "image/png", "image", user!!.imageId)
        compose(observer, Constant.HTTP_KEY_UPLOAD_FILE, SysFile::class.java)
    }

    /**
     * 下载文件
     */
    fun downloadImage(id: Long, key: Int) {
        val observer = repository.downloadImage(id)
        composeFile(observer, key)
    }

}
